/**
 * 
 */
package org.springside.examples.miniweb.web.report.excel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import org.springside.examples.miniweb.entity.report.ColdPumpReport;

/**
 * @author Eric 2012-3-18
 * 
 * @since 6.0
 * 
 */
public class ColdReportExcelView extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String string = "冷泵维护列表"+ DateFormatUtils.format(new Date(),
						"yyyy-MM-dd")+".xls";
		response.setHeader("Content-Disposition", "attachment; filename="+ 
				new String(string.getBytes(), "iso8859-1")); 
		@SuppressWarnings("unchecked")
		List<ColdPumpReport> coldList = (List<ColdPumpReport>) model.get("coldReports");
		coldList = coldList == null ? new ArrayList<ColdPumpReport>() : coldList; 
		HSSFSheet sheet = workbook.createSheet("冷泵维护列表");
		HSSFRow header = sheet.createRow(0);
		String[] headers = new String[]{"维修ID","重点说明","客户名称","S/N","安排人员","预计完成时间","入库","制冷机拆卸","真空端拆卸","制冷机清洗",
				"真空端清洗","制冷机组装","活塞组装","真空端组装","降温测试","气体测试","完修包装","出库","备注","更新时间"};
		for (int i = 0; i < headers.length; i++) {
			header.createCell(i).setCellValue(headers[i]);
		}

		int rowNum = 1;
		for (ColdPumpReport report : coldList) {
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(report.getReportId());
			row.createCell(1).setCellValue(report.getKeyComment());
			row.createCell(2).setCellValue(report.getCustomerName());
			row.createCell(3).setCellValue(report.getSN());
			row.createCell(4).setCellValue(report.getAssignedUser());
			row.createCell(5).setCellValue(report.getScheduleFinish() == null ? "" : DateFormatUtils.format(report.getScheduleFinish(),
					"yyyy-MM-dd"));
			row.createCell(6).setCellValue(report.getInputRepository() == null ? "" : DateFormatUtils.format(report.getInputRepository(),
					"yyyy-MM-dd"));
			row.createCell(7).setCellValue( report.getColdDismantle() == null ? "" :DateFormatUtils.format(report.getColdDismantle(),
					"yyyy-MM-dd"));
			row.createCell(8).setCellValue( report.getEmptyDismantle() == null ? "" :DateFormatUtils.format(report.getEmptyDismantle(),
					"yyyy-MM-dd"));
			row.createCell(9).setCellValue( report.getColdClean() == null ? "" :DateFormatUtils.format(report.getColdClean(),
					"yyyy-MM-dd"));
			row.createCell(10).setCellValue( report.getEmptyClean() == null ? "" :DateFormatUtils.format(report.getEmptyClean(),
					"yyyy-MM-dd"));
			row.createCell(11).setCellValue( report.getColdAssemble() == null ? "" :DateFormatUtils.format(report.getColdAssemble(),
					"yyyy-MM-dd"));
			row.createCell(12).setCellValue(report.getPistonAssemble() == null ? "" : DateFormatUtils.format(report.getPistonAssemble(),
					"yyyy-MM-dd"));
			row.createCell(13).setCellValue( report.getEmptyAssemble() == null ? "" :DateFormatUtils.format(report.getEmptyAssemble(),
					"yyyy-MM-dd"));
			row.createCell(14).setCellValue( report.getDownTempTest() == null ? "" :DateFormatUtils.format(report.getDownTempTest(),
					"yyyy-MM-dd"));
			row.createCell(15).setCellValue( report.getGasTest() == null ? "" :DateFormatUtils.format(report.getGasTest(),
					"yyyy-MM-dd"));
			row.createCell(16).setCellValue(report.getFinishPackage() == null ? "" : DateFormatUtils.format(report.getFinishPackage(),
					"yyyy-MM-dd"));
			row.createCell(17).setCellValue(report.getOutputRepository() == null ? "" : DateFormatUtils.format(report.getOutputRepository(),
					"yyyy-MM-dd"));
			row.createCell(18).setCellValue(report.getComments());
			row.createCell(19).setCellValue(report.getUpdateTime() == null ? "" : DateFormatUtils.format(report.getUpdateTime(),
					"yyyy-MM-dd"));
		}
	}

}
